package com.gd.data.platform.mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gd.data.platform.entity.DownloadLog;

/**
 * 数据下载日志Mapper
 * 操作ClickHouse中的下载日志数据
 * 
 * @author gd-data-platform
 * @since 1.0.0
 */
@Mapper
public interface DownloadLogMapper extends BaseMapper<DownloadLog> {

    /**
     * 统计指定时间窗口内的下载量
     */
    @Select("SELECT COUNT(*) as download_count FROM download_logs " +
            "WHERE download_time >= #{startTime} AND download_time <= #{endTime}")
    Long countDownloadsByTimeRange(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 统计指定IP的下载量
     */
    @Select("SELECT COUNT(*) as download_count FROM download_logs " +
            "WHERE client_ip = #{clientIp} AND download_time >= #{startTime} AND download_time <= #{endTime}")
    Long countDownloadsByIp(@Param("clientIp") String clientIp,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 统计指定资源的下载量
     */
    @Select("SELECT COUNT(*) as download_count FROM download_logs " +
            "WHERE resource_id = #{resourceId} AND download_time >= #{startTime} AND download_time <= #{endTime}")
    Long countDownloadsByResource(@Param("resourceId") String resourceId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 获取资源下载排行
     */
    @Select("SELECT resource_id, resource_name, resource_type, COUNT(*) as download_count " +
            "FROM download_logs " +
            "WHERE download_time >= #{startTime} AND download_time <= #{endTime} " +
            "GROUP BY resource_id, resource_name, resource_type " +
            "ORDER BY download_count DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> getResourceDownloadRankings(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("limit") Integer limit);

    /**
     * 获取沉睡资源（近3个月调用量小于阈值）
     */
    @Select("SELECT resource_id, resource_name, resource_type, COUNT(*) as call_count, " +
            "MAX(download_time) as last_download_time " +
            "FROM download_logs " +
            "WHERE download_time >= #{startTime} " +
            "GROUP BY resource_id, resource_name, resource_type " +
            "HAVING call_count < #{threshold}")
    List<Map<String, Object>> getSleepResources(@Param("startTime") LocalDateTime startTime,
            @Param("threshold") Long threshold);

    /**
     * 统计下载文件总大小
     */
    @Select("SELECT SUM(file_size) as total_size " +
            "FROM download_logs " +
            "WHERE download_time >= #{startTime} AND download_time <= #{endTime} AND download_status = '成功'")
    Long getTotalDownloadSize(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
}
